Public Class OrderStatusReportCtrl
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents litReport As System.Web.UI.WebControls.Literal

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

#Region "    Properties"
    Public Property Year() As Integer
        Get
            Return CInt(ViewState("Year"))
        End Get
        Set(ByVal Value As Integer)
            ViewState("Year") = Value
        End Set
    End Property

    Public Property Week() As Integer
        Get
            Return CInt(ViewState("Week"))
        End Get
        Set(ByVal Value As Integer)
            ViewState("Week") = Value
        End Set
    End Property

    Public Property SupplierID() As Integer
        Get
            Return CInt(ViewState("SupplierID"))
        End Get
        Set(ByVal Value As Integer)
            ViewState("SupplierID") = Value
        End Set
    End Property

    Public Property ETAPortID() As Integer
        Get
            Return CInt(ViewState("ETAPortID"))
        End Get
        Set(ByVal Value As Integer)
            ViewState("ETAPortID") = Value
        End Set
    End Property

    ' 0: Screen (View)
    ' 1: Printer (Print)
    ' 2: File (Export)
    Public Property Output() As Report.EReportOutputType
        Get
            Return CType(ViewState("Output"), Report.EReportOutputType)
        End Get
        Set(ByVal Value As Report.EReportOutputType)
            ViewState("Output") = Value
        End Set
    End Property
#End Region

    Public Sub GenerateReport()
        Me.litReport.Text = Me.GetReportHTML
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Function GetReportHeaderHTML() As String
        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR>")
        sb.Append("<TD align=center colspan=13>")
        sb.AppendFormat("ORDER STATUS REPORT")
        sb.Append("</TD>")
        sb.Append("</TR>")

        Return sb.ToString
    End Function

    Public Function GetReportHTML() As String
        Dim sb As New System.Text.StringBuilder
        sb.Append("<TABLE cellSpacing=1 cellPadding=3 width=""100%"" border=0>")

        If Me.Output <> Report.EReportOutputType.Screen Then
            sb.Append(Me.GetReportHeaderHTML)
        End If

        sb.Append(Me.GetTHeadHTML)
        sb.Append(Me.GetTBodyHTML)
        sb.Append("</TABLE>")
        Return sb.ToString
    End Function

    Protected Function GetTHeadHTML() As String
        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR align=""center"">")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Bulk</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Supplier Sort Ref.</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Date of Order</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Qty</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>% Completed</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Customer No.</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>ETD</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Vessel</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>ETA Port</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>ETA</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Doc's Sent</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Payment Received</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=""8%""><strong>Invoice No</strong></TD>")
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<THEAD>")
            sb.Append("</THEAD>")
        End If

        Return sb.ToString
    End Function

    Protected Function GetTBodyHTML() As String

        Dim dt As DataTable
        dt = COrderStatus.GetOrderStatusReport(Me.SupplierID, Me.ETAPortID, Me.Week, Me.Year)

        Dim sb As New System.Text.StringBuilder

        Dim dr As DataRow
        For Each dr In dt.Rows
            Dim objO As New COrder
            objO.Load(dr("ORDER_ID_FK"))

            Select Case CType(dr("STATUS"), Report.EReportOrderStatus)
                Case Report.EReportOrderStatus.Additions
                    sb.Append("<tr align=center class=bgblue>")
                Case Report.EReportOrderStatus.Archived
                    sb.Append("<tr align=center class=bgkhaki>")
                Case Report.EReportOrderStatus.Removed
                    sb.Append("<tr align=center class=bggrey>")
                Case Report.EReportOrderStatus.Changes
                    sb.Append("<tr align=center class=bggreen>")
                Case Report.EReportOrderStatus.No_changes
                    sb.Append("<tr align=center class=bgwhite>")
            End Select

            sb.AppendFormat("<td>{0}</td>", IIf(objO.IsIndependentPO, "", "*"))
            sb.AppendFormat("<td>{0}</td>", objO.SortReference)
            sb.AppendFormat("<td>{0}</td>", FormatDate(objO.OrderDate))
            sb.AppendFormat("<td>{0}</td>", String.Format("{0} x{1}", objO.Containers, CType(objO.ContainerType, EContainerType).ToString.Replace("_", " ")))
            sb.AppendFormat("<td>{0}</td>", dr("PERCENT_COMPLETED"))
            sb.AppendFormat("<td>{0}</td>", objO.OrderNumber)
            sb.AppendFormat("<td>{0}</td>", FormatDate(CDate(dr("EST_TIME_DELIVERY"))))
            sb.AppendFormat("<td>{0}</td>", dr("VESSEL"))
            sb.AppendFormat("<td>{0}</td>", objO.DestinationPort)
            sb.AppendFormat("<td>{0}</td>", FormatDate(CDate(dr("EST_TIME_ARRIVAL"))))
            sb.AppendFormat("<td>{0}</td>", IIf(CBool(dr("DOC_SENT")) = True, "Yes", ""))
            sb.AppendFormat("<td>{0}</td>", IIf(CBool(dr("PAY_RECEIVED")) = True, "Yes", ""))
            sb.AppendFormat("<td>{0}</td>", dr("INVOICE_NO"))

            sb.Append("</tr>")
        Next

        Return sb.ToString
    End Function
End Class
